<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class StudyStage extends Model
{
    protected $table = 'study_stage'; 
    protected $guarded = [];
    protected $appends = [
        'stage_set',
        'status_set',
        'resources_set'
    ];

    public function getStageSetAttribute(){
        $result = "";
        $stage = !empty($this->attributes['stage'])?$this->attributes['stage']:'';

        switch($stage) {
            case 1:
                $result = '申报阶段';
                break;
            case 2:
                $result = '立项阶段';
                break;
            case 3:
                $result = '中期阶段';
                break;
            case 4:
                $result = '结项阶段';
                break;
        }
        
        return $result;
    } 

    public function getStatusSetAttribute(){
        $result = "待提交";
        $status = !empty($this->attributes['status'])?$this->attributes['status']:'';

        switch($status) {
            case 1:
                $result = '待审核';
                break;
            case 2:
                $result = '通过';
                break;
            case 3:
                $result = '已驳回';
                break;
        }

        return $result;
    } 
    

    public function getResourcesSetAttribute(){
        $resources = !empty($this->attributes['resources'])?$this->attributes['resources']:'';
        if(!$resources){
            return [];
        }

        $files = json_decode($resources, true);
        foreach($files as $k => $f) {
            $file = File::whereId($f['id'])->first();
            
            $files[$k]['file_info'] = $file->toArray();
            $files[$k]['file_origin'] = [
                'file_name' => $f['fileName'],
                'size' => $file->file_size,
                'url' => $file->file_dir . $file->file_path,
                'id' => $f['id']
            ];
        }

        return $files;
    } 
    
}
